package cn.crism.blog.controller;

import cn.crism.blog.conver.CommentConver;
import cn.crism.blog.param.CommentParam;
import cn.crism.blog.param.QueryParam;
import cn.crism.blog.service.CommentService;
import cn.crism.blog.service.ReplyService;
import cn.crism.blog.util.ResultVOUtil;
import cn.crism.blog.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @Author: Crism
 * @Date: 上午9:28 18/2/7
 * @Description:
 */
@RestController
@RequestMapping("/api/bg")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;

    @PostMapping("/getComments")
    public ResultVO getComments(@RequestBody @Valid QueryParam queryParam) {
        Map<String, Object> comments = commentService.getComments(queryParam);
        return comments != null ? ResultVOUtil.success(comments) : ResultVOUtil.error(1,"获取失败！");
    }

    @PostMapping("/getReply")
    public ResultVO getReply(@RequestBody @Valid QueryParam queryParam) {
        Map<String, Object> reply = replyService.getReplySByComment(queryParam);
        return reply != null ? ResultVOUtil.success(reply) : ResultVOUtil.error(1,"获取失败！");
    }

    @GetMapping("/accessComment")
    public ResultVO accessComment(@RequestParam("id") String commentId) {
        return commentService.accessComment(commentId)? ResultVOUtil.success("审核成功！") : ResultVOUtil.error(1,"审核失败！");
    }

    @GetMapping("/accessReply")
    public ResultVO accessReply(@RequestParam("id") String replyId) {
        return replyService.accessReply(replyId)? ResultVOUtil.success("审核成功！") : ResultVOUtil.error(1,"审核失败！");
    }

    @GetMapping("/removeComment")
    public ResultVO removeComment(@RequestParam("id") String commentId) {
        return commentService.removeComment(commentId) != null ? ResultVOUtil.success("删除成功！") : ResultVOUtil.error(1,"删除失败！");
    }

    @GetMapping("/removeReply")
    public ResultVO removeReply(@RequestParam("id") String replyId) {
        return replyService.removeReply(replyId) != null ? ResultVOUtil.success("删除成功！") : ResultVOUtil.error(1,"删除失败！");
    }

    @PostMapping("/comment")
    public ResultVO comment(@RequestBody @Valid CommentParam commentParam) {
        return commentService.createComment(CommentConver.toComment(commentParam))? ResultVOUtil.success("评论成功，等待审核！") : ResultVOUtil.error(1,"评论失败！");
    }

    @PostMapping("/reply")
    public ResultVO reply(@RequestBody @Valid CommentParam commentParam) {
        return replyService.createReply(CommentConver.toReply(commentParam),commentParam.getCommentId())? ResultVOUtil.success("评论成功，等待审核！") : ResultVOUtil.error(1,"评论失败！");
    }
}
